<?php
    
  require_once "JooS/UCS2.php";
    
  class JooS_UCS2_UTF_8 extends JooS_UCS2 {
    public function convertString($str) {
      $letters = $this->utf8_str_split($str);
      $isWin = (isset($_ENV["ComSpec"]) || isset($_SERVER["COMSPEC"]) || isset($_SERVER["WINDIR"]) || isset($_ENV["windir"]));
      $i0 = $isWin ? 0 : 1;
      $i1 = $isWin ? 1 : 0;

      $result = "";
      for ($i=0; $i<sizeof($letters); $i++) {
        $char = $letters[$i];
        if (strlen($char) > 1) {
          $char = iconv("UTF-8", "UCS-2", $char);
          $c0 = dechex(ord($char[$i0]));
          $c1 = dechex(ord($char[$i1]));
          $char = "\\u" . str_pad($c0, 2, "0", STR_PAD_LEFT) . str_pad($c1, 2, "0", STR_PAD_LEFT);
        }
        $result .= $char;
      }
      return $result;
    }

  
    /**
     * Implementation str_split() function for UTF-8 encoding string.
     *
     * @created  2008-12-15
     * @license  http://creativecommons.org/licenses/by-sa/3.0/
     * @author   Nasibullin Rinat <nasibullin at starlink ru>
     * @charset  ANSI
     * @version  1.0.1
     */
    private function utf8_str_split(/*string*/ $string, /*int*/ $length = null) {
      $length = ($length === null) ? 1 : intval($length);
      if ($length < 1)
        return false;

      if ($length < 100) {
        preg_match_all('/(?>[\x09\x0A\x0D\x20-\x7E]           # ASCII
                          | [\xC2-\xDF][\x80-\xBF]            # non-overlong 2-byte
                          |  \xE0[\xA0-\xBF][\x80-\xBF]       # excluding overlongs
                          | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
                          |  \xED[\x80-\x9F][\x80-\xBF]       # excluding surrogates
                          |  \xF0[\x90-\xBF][\x80-\xBF]{2}    # planes 1-3
                          | [\xF1-\xF3][\x80-\xBF]{3}         # planes 4-15
                          |  \xF4[\x80-\x8F][\x80-\xBF]{2}    # plane 16
                          #| (.)                               # catch bad bytes
                         ){1,' . $length . '}
                        /xsS', $string, $m);
        $a =& $m[0];
      }
      else {
        preg_match_all('/(?>[\x09\x0A\x0D\x20-\x7E]           # ASCII
                          | [\xC2-\xDF][\x80-\xBF]            # non-overlong 2-byte
                          |  \xE0[\xA0-\xBF][\x80-\xBF]       # excluding overlongs
                          | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
                          |  \xED[\x80-\x9F][\x80-\xBF]       # excluding surrogates
                          |  \xF0[\x90-\xBF][\x80-\xBF]{2}    # planes 1-3
                          | [\xF1-\xF3][\x80-\xBF]{3}         # planes 4-15
                          |  \xF4[\x80-\x8F][\x80-\xBF]{2}    # plane 16
                          #| (.)                               # catch bad bytes
                         )
                        /xsS', $string, $m);
        $a = array();
        for ($i = 0, $c = count($m[0]); $i < $c; $i += $length)
          $a[] = implode('', array_slice($m[0], $i, $length));
      }

      $distance = strlen($string) - strlen(implode('', $a));
      if ($distance > 0) {
        return false;
      }
      return $a;
    }
  }

?>